-module(downloadwebpage).
-export([start/0, start/1]).

start() ->
	start("www.google.com").
	
start(Host) ->
	case gen_tcp:connect(Host, 80, [binary, {packet, 0}]) of
		{ok, Socket} ->
			gen_tcp:send(Socket, "GET / HTTP/1.0\r\n\r\n"),
			receive_data(Socket, []);
		{error, Reason} ->
			io:fwrite("Error: ~w~n", [Reason])
	end.
	
receive_data(Socket, SoFar) ->
	receive
		{tcp, Socket, Bin} ->
			receive_data(Socket, [Bin|SoFar]);
		{tcp_closed, Socket} ->
			list_to_binary(lists:reverse(SoFar))
	end.